package com.frxuexi.myroom.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.frxuexi.lib_base.BaseViewModel
import com.frxuexi.myroom.intent.LeaveIntent
import com.frxuexi.myroom.paging.MyDataSource
import com.frxuexi.myroom.repository.LeaveRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class LeaveViewModel @Inject constructor(private val leaveRepo: LeaveRepo):BaseViewModel<LeaveIntent>() {
    override fun handleIntent(it: LeaveIntent) {
        when(it){
            is LeaveIntent.addLeave->{
                roomRequest(leaveRepo.addLeave(it.leave))
            }
            is LeaveIntent.selectNoApprove->{
                roomRequest(leaveRepo.selectNoApprove(it.status))
            }
            is LeaveIntent.update->{
                roomRequest(leaveRepo.update(it.leave))
            }
        }
    }

    fun getDatas(uid:Int)=Pager(PagingConfig(pageSize = 1)){
        MyDataSource(leaveRepo,uid)
    }.flow.cachedIn(viewModelScope)
}